package com.hnchances.domain.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {

    private static final int OK = 200;

    private int code; //编码：1成功，0和其它数字为失败
    private String msg; //错误信息
    private T result; //数据

    private Result (int code,String msg,T result){
        this.code=code;
        this.msg=msg;
        this.result=result;
    }

    private Result (int code,String msg){
        this.code=code;
        this.msg=msg;
    }

    private Result (Code code,T result){
        this.code=code.getCode();
        this.msg=code.getMsg();
        this.result=result;
    }

    private Result (Code code){
        this.code=code.getCode();
        this.msg=code.getMsg();
    }


    public static <T> Result<T> ok() {
        return new Result<T>(CodeEnum.TOW_OK,null);
    }
    public static <T> Result<T> ok(T obkect) {
        return new Result<T>(CodeEnum.TOW_OK,obkect);
    }

    public static <T> Result<T> error(CodeEnum codeEnum) {
        return new Result<>(codeEnum);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<>(OK,msg);
    }

}
